package com.party.course.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.date.DateUtil;
import com.party.business.domain.KcType;
import com.party.business.mapper.KcTypeMapper;
import com.party.common.core.domain.entity.SysUser;
import com.party.course.domain.PayGroup;
import com.party.course.mapper.PayGroupMapper;
import com.party.course.util.DateTimeUtill;
import org.jacoco.agent.rt.internal_1f1cc91.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.party.course.mapper.BearInfoMapper;
import com.party.course.domain.BearInfo;
import com.party.course.service.IBearInfoService;

import cn.hutool.core.util.IdUtil;

import javax.annotation.Resource;

/**
 * 承担量信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class BearInfoServiceImpl extends ServiceImpl<BearInfoMapper, BearInfo> implements IBearInfoService {
    @Resource
    private BearInfoMapper bearInfoMapper;
    @Resource
    private PayGroupMapper payGroupMapper;
    @Resource
    private KcTypeMapper kcTypeMapper;

    /**
     * 查询可用的承担量
     *
     * @param userList
     * @param expectTime
     * @return
     */
    @Override
    public BearInfo findUseBear(HashSet<SysUser> userList, String expectTime, String isTest) {
        if (StrUtil.isEmpty(expectTime)) {
            throw new RuntimeException("缺少期望上课时间！");
        }
        //处理期望上课的时间格式
        String[] parts = expectTime.split(" ");
        String date = parts[0];
        String timePart = parts[1];
        // 转换日期为星期几
        String weekDay = DateTimeUtill.getWeek(date);
        // 转换时间为 12 小时制格式
//        String paramTime = DateTimeUtill.formatTime(timePart);
        String paramTime = timePart;
        for (SysUser user : userList) {
            BearInfo bearInfo = null;
            if ("1".equals(isTest)) {
                bearInfo = bearInfoMapper.findUseByUserIdTest(user.getUserId().toString(),weekDay, paramTime);
            } else {
                bearInfo = bearInfoMapper.findUseByUserId(user.getUserId().toString(), weekDay,paramTime);
            }
            if (ObjectUtil.isNotEmpty(bearInfo)) {
                List<PayGroup> groups = new ArrayList<>();
                //承担量创建人与交付小组的创建人是同一个交付中心
                if ("1".equals(isTest)) {
                    groups.addAll(payGroupMapper.findUseByUserIdTest(bearInfo.getCreateBy()));
                } else {
                    groups.addAll(payGroupMapper.findUseByUserId(bearInfo.getCreateBy()));
                }
                if (!groups.isEmpty()) {
                    bearInfo.setPayGroup(groups.get(0));
                    return bearInfo;
                }
            }
        }
        return null;
    }

    @Override
    public void subBear(BearInfo bearInfo, String isTest) {
        if (ObjectUtil.isNotEmpty(bearInfo) && ObjectUtil.isNotEmpty(bearInfo.getPayGroup())) {
            PayGroup payGroup = bearInfo.getPayGroup();
            if ("1".equals(isTest)) {
                Integer testMax = bearInfo.getTestMax();
                if (testMax > 0) {
                    bearInfo.setTestMax(testMax - 1);
                }
                Integer testUse = bearInfo.getTestUse();
                bearInfo.setTestUse(testUse + 1);
                Integer max = payGroup.getTestMax();
                if (max > 0) {
                    payGroup.setTestMax(max - 1);
                }
                Integer use = payGroup.getTestUse();
                payGroup.setTestUse(use + 1);
            } else {
                Integer max = bearInfo.getFormalMax();
                if (max > 0) {
                    bearInfo.setFormalMax(max - 1);
                }
                Integer use = bearInfo.getFormalUse();
                bearInfo.setFormalUse(use + 1);
                Integer max2 = payGroup.getFormalMax();
                if (max2 > 0) {
                    payGroup.setFormalMax(max2 - 1);
                }
                Integer use2 = payGroup.getFormalUse();
                payGroup.setFormalUse(use2 + 1);
            }
            payGroupMapper.updateById(payGroup);
            bearInfoMapper.updateById(bearInfo);
        }
    }

    /**
     * 查询承担量信息
     *
     * @param id 承担量信息主键
     * @return 承担量信息
     */
    @Override
    public BearInfo view(String id) {
        return bearInfoMapper.selectBearInfoById(id);
    }

    /**
     * 查询承担量信息列表
     *
     * @param bearInfo 承担量信息
     * @return 承担量信息
     */
    @Override
    public List<BearInfo> findList(BearInfo bearInfo) {
        return bearInfoMapper.selectBearInfoList(bearInfo);
    }

    /**
     * 新增承担量信息
     *
     * @param bearInfo 承担量信息
     * @return 结果
     */
    @Override
    public int add(BearInfo bearInfo) {
        bearInfo.setId(IdUtil.simpleUUID());
        bearInfo.setDelFlag("0");
        setClassType(bearInfo);
        return bearInfoMapper.insert(bearInfo);
    }

    public void setClassType(BearInfo bearInfo) {
        //课程分类
        if (StrUtil.isNotEmpty(bearInfo.getClassTypeId())) {
            KcType kcType = kcTypeMapper.selectKcTypeById(bearInfo.getClassTypeId());
            if (ObjectUtil.isNotEmpty(kcType)) {
                bearInfo.setClassTypeName(kcType.getName());
            }
        }
    }

    /**
     * 修改承担量信息
     *
     * @param bearInfo 承担量信息
     * @return 结果
     */
    @Override
    public int updateEntity(BearInfo bearInfo) {
        bearInfo.setUpdateTime(DateUtil.date());
        setClassType(bearInfo);
        return bearInfoMapper.updateById(bearInfo);
    }

    /**
     * 批量删除承担量信息
     *
     * @param ids 需要删除的承担量信息主键
     * @return 结果
     */
    @Override
    public int deleteIds(List<String> ids) {
        return bearInfoMapper.deleteBearInfoByIds(ids);
    }

    /**
     * 删除承担量信息信息
     *
     * @param id 承担量信息主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return bearInfoMapper.deleteBearInfoById(id);
    }
}
